//generer un xml a partir du spf dataprojectlong
package expressToAltaRica;

import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.example.MFHAActivator;
import org.example.MFHADataflow;
import org.example.MFHAEffectiveness;
import org.example.MFHAFct;
import org.example.MFHAGraph;
import org.example.MFHAInInterface;
import org.example.MFHALink;
import org.example.MFHAObs;
import org.example.MFHAOp;
import org.example.MFHAOutInterface;
import org.example.MFHAStructure;
import org.example.ObjectFactory;



import jsdai.SMfha_graph_schema.AMfha_activator;
import jsdai.SMfha_graph_schema.AMfha_dataflow;
import jsdai.SMfha_graph_schema.AMfha_fct;
import jsdai.SMfha_graph_schema.AMfha_graph;
import jsdai.SMfha_graph_schema.AMfha_in_interface;
import jsdai.SMfha_graph_schema.AMfha_link;
import jsdai.SMfha_graph_schema.AMfha_obs;
import jsdai.SMfha_graph_schema.AMfha_op;
import jsdai.SMfha_graph_schema.AMfha_out_interface;
import jsdai.SMfha_graph_schema.AMfha_structure;
import jsdai.SMfha_graph_schema.EMfha_activator;
import jsdai.SMfha_graph_schema.EMfha_dataflow;
import jsdai.SMfha_graph_schema.EMfha_fct;
import jsdai.SMfha_graph_schema.EMfha_graph;
import jsdai.SMfha_graph_schema.EMfha_in_interface;
import jsdai.SMfha_graph_schema.EMfha_link;
import jsdai.SMfha_graph_schema.EMfha_obs;
import jsdai.SMfha_graph_schema.EMfha_op;
import jsdai.SMfha_graph_schema.EMfha_out_interface;
import jsdai.SMfha_graph_schema.EMfha_structure;
import jsdai.lang.AEntity;
import jsdai.lang.ASdaiModel;
import jsdai.lang.EEntity;
import jsdai.lang.SdaiException;
import jsdai.lang.SdaiIterator;
import jsdai.lang.SdaiModel;
import jsdai.lang.SdaiRepository;
import jsdai.lang.SdaiSession;
import jsdai.lang.SdaiTransaction;
import jsdai.lang.Value;

public class AfficherXml {

	public static void main(String[] args) throws SdaiException, Exception{
		java.util.Properties prop = new java.util.Properties();
		prop.setProperty("repositories","D:\\Repositories119");
		SdaiSession.setSessionProperties(prop);
		SdaiSession session = SdaiSession.openSession();
		SdaiTransaction transaction = session.startTransactionReadWriteAccess();
		SdaiRepository repository = session.importClearTextEncoding("MyRepo", "D:\\dataprojetlong.stp", null);
		if(!repository.isActive()){
			repository.openRepository();
		}
		transaction.commit();
		ASdaiModel models = repository.getModels();
		SdaiIterator modelIterator = models.createIterator();
		while(modelIterator.next()){
			SdaiModel model = models.getCurrentMember(modelIterator);
			if(model.getMode() == SdaiModel.NO_ACCESS){
				model.startPartialReadOnlyAccess();
			}
			System.out.println("Model found: " + model.getName());
			/*AFemale females = (AFemale)model.getInstances(EFemale.class);
			SdaiIterator femalesIterator = females.createIterator();
			while(femalesIterator.next()){
				EFemale female = females.getCurrentMember(femalesIterator);
				System.out.println("Name:"+female.getName(null));
			}*/
			//Instancier l'objet Factory
			ObjectFactory objFactory = new ObjectFactory();
			//MFHA_Graph
			System.out.println("MFHA_GRAPH ");
	/*		MFHAActivator mfhaactivator_obj = new MFHAActivator();
			AMfha_activator mfha_activators = (AMfha_activator)model.getInstances(EMfha_activator.class);
			SdaiIterator mfha_activators_Iterator = mfha_activators.createIterator();
			while(mfha_activators_Iterator.next()){
				EMfha_activator mfha_activator = mfha_activators.getCurrentMember(mfha_activators_Iterator);
				MFHAOutInterface aux = new MFHAOutInterface();
				aux.setName(mfha_activator.getName(null));
				mfhaactivator_obj.setAct(aux);
			}*/
			//m�thode: on parcours d'abord toutes les entit�s Emfhagraph, et pour chaque entit� on 
			//transmet les donn�es dedans � mfhagraph_obj
			MFHAGraph mfhagraph_obj = objFactory.createMFHAGraph();
			AMfha_graph mfha_graphs = (AMfha_graph)model.getInstances(EMfha_graph.class);
			SdaiIterator mfha_graphs_Iterator = mfha_graphs.createIterator();
			while(mfha_graphs_Iterator.next()){
				//notre entite est representee par mfha_graph
				EMfha_graph mfha_graph = mfha_graphs.getCurrentMember(mfha_graphs_Iterator);
				//apres on prend tous les activators de notre entite, et on les transmet au mfhagraph_obj
				AMfha_activator mfha_activators = mfha_graph.getActivators(null);
				SdaiIterator mfha_activators_Iterator = mfha_activators.createIterator();
				MFHAActivator mfhaactivator_obj = objFactory.createMFHAActivator();
				while(mfha_activators_Iterator.next()){
					EMfha_activator mfha_activator = mfha_activators.getCurrentMember(mfha_activators_Iterator);
					String name = mfha_activator.getName(null);
					mfhaactivator_obj.setName(name);
					mfhaactivator_obj.setType("MFHA_activator");
					EMfha_out_interface mfha_out_interfaces = mfha_activator.getAct(null);
					MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
					mfhaoutinterface_obj.setName(mfha_out_interfaces.getName(null));
					mfhaoutinterface_obj.setType("MFHA_out_interface");
					mfhaactivator_obj.setAct(mfhaoutinterface_obj);
					}
					mfhagraph_obj.getActivator().add(mfhaactivator_obj);
					//Comme ca on a rempli l'activator
					
					//On passe maintenant au mfha_fct
					AMfha_fct mfha_fcts = mfha_graph.getFcts(null);
					SdaiIterator mfha_fcts_Iterator = mfha_fcts.createIterator();
					MFHAFct mfhafct_obj = objFactory.createMFHAFct();
					while(mfha_fcts_Iterator.next()){
						EMfha_fct mfha_fct = mfha_fcts.getCurrentMember(mfha_fcts_Iterator);
						String name = mfha_fct.getName(null);
						mfhafct_obj.setName(name);
						mfhafct_obj.setType("MFHA_fct");
						int effectiveness = mfha_fct.getEffectiveness(null);
						
						//dataU du Fct
						AMfha_in_interface mfha_in_interfaces = mfha_fct.getData_u(null);
						SdaiIterator mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
						SdaiIterator aux = mfha_in_interfaces_Iterator;
						if(!aux.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhafct_obj.getDataU().add(mfhaininterface_obj);
						}
						else{
						while(mfha_in_interfaces_Iterator.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							EMfha_in_interface mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhafct_obj.getDataU().add(mfhaininterface_obj);
						}
						}
						
						//dataN du Fct
						mfha_in_interfaces = mfha_fct.getData_n(null);
						mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
						aux = mfha_in_interfaces_Iterator;
						if(!aux.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhafct_obj.getDataN().add(mfhaininterface_obj);
						}
						else{
						while(mfha_in_interfaces_Iterator.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							EMfha_in_interface mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhafct_obj.getDataN().add(mfhaininterface_obj);
						}
						}
						
						//envData du Fct
						mfha_in_interfaces = mfha_fct.getEnv_data(null);
						mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
						aux = mfha_in_interfaces_Iterator;
						if(!aux.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhafct_obj.getDataN().add(mfhaininterface_obj);
						}
						else{
						while(mfha_in_interfaces_Iterator.next()){
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							EMfha_in_interface mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhafct_obj.getEnvData().add(mfhaininterface_obj);
						}
						}
						
						
						EMfha_out_interface mfha_out_interface = mfha_fct.getOut_data(null);
						MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
						mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
						mfhaoutinterface_obj.setType("MFHA_out_interface");
						mfhafct_obj.setOutData(mfhaoutinterface_obj);
						}
						mfhagraph_obj.getFct().add(mfhafct_obj);
					//apres on passe au MFHA_LINK
						AMfha_link mfha_links = mfha_graph.getLinks(null);
						SdaiIterator mfha_links_Iterator = mfha_links.createIterator();
						

						while(mfha_links_Iterator.next()){
							MFHALink mfhalink_obj = objFactory.createMFHALink();
							mfhalink_obj.setType("MFHA_link");
							EMfha_link mfha_link = mfha_links.getCurrentMember(mfha_links_Iterator);
							EMfha_out_interface mfha_out_interface = mfha_link.getInit_interface(null);
							EMfha_in_interface mfha_in_interface = mfha_link.getFinal_interface(null);
							MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhaoutinterface_obj.setType("MFHA_out_interface");
							mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							//System.out.println("name: " + mfha_out_interface.getName(null));
							mfhalink_obj.setInitInterface(mfhaoutinterface_obj);
							mfhalink_obj.setFinishInterface(mfhaininterface_obj);
							//System.out.println("name2: " + (mfhalink_obj.getInitInterface()).getName());
							
							mfhagraph_obj.getLink().add(mfhalink_obj);
						}
						//apres on passe a MFHA_OBS
						AMfha_obs mfha_obss = mfha_graph.getObs(null);
						SdaiIterator mfha_obss_Iterator = mfha_obss.createIterator();
						while(mfha_obss_Iterator.next()){
							MFHAObs mfhaob_obj = objFactory.createMFHAObs();
							mfhaob_obj.setType("MFHA_obs");
							EMfha_obs mfha_obs = mfha_obss.getCurrentMember(mfha_obss_Iterator);
							EMfha_in_interface mfha_in_interface = mfha_obs.getObs(null);
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							mfhaob_obj.setObs(mfhaininterface_obj);
							mfhagraph_obj.getOb().add(mfhaob_obj);
						}
						
						//apres on passe a MFHA_STRUCTURE
						AMfha_structure mfha_structures = mfha_graph.getStructures(null);
						SdaiIterator mfha_structures_Iterator = mfha_structures.createIterator();
						while(mfha_structures_Iterator.next()){
							MFHAStructure mfhastructure_obj = objFactory.createMFHAStructure();
							mfhastructure_obj.setType("MFHA_structure");
							EMfha_structure mfha_structure = mfha_structures.getCurrentMember(mfha_structures_Iterator);
							mfhastructure_obj.setName(mfha_structure.getName(null));
							
							//InActivation du structure
							AMfha_in_interface mfha_in_interfaces = mfha_structure.getIn_activation(null);
							SdaiIterator mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
							SdaiIterator aux = mfha_in_interfaces.createIterator();
							if(!aux.next()){
								MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
								mfhastructure_obj.getInActivation().add(mfhaininterface_obj);
							}
							else{
							while(mfha_in_interfaces_Iterator.next()){
								MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
								EMfha_in_interface mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
								mfhaininterface_obj.setName(mfha_in_interface.getName(null));
								mfhaininterface_obj.setType("MFHA_in_interface");
								mfhastructure_obj.getInActivation().add(mfhaininterface_obj);
							}
							}
							
							//OutActivation du structure
							AMfha_out_interface mfha_out_interfaces = mfha_structure.getOut_activation(null);
							SdaiIterator mfha_out_interfaces_Iterator = mfha_out_interfaces.createIterator();
							aux = mfha_out_interfaces.createIterator();
							if(!aux.next()){
								MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
								mfhastructure_obj.getOutActivation().add(mfhaoutinterface_obj);
							}
							else{
							while(mfha_out_interfaces_Iterator.next()){
								MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
								EMfha_out_interface mfha_out_interface = mfha_out_interfaces.getCurrentMember(mfha_out_interfaces_Iterator);
								mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
								System.out.println(mfha_out_interface.getName(null));
								mfhaoutinterface_obj.setType("MFHA_out_interface");
								mfhastructure_obj.getOutActivation().add(mfhaoutinterface_obj);
							}
							}
							
							mfhagraph_obj.getStructure().add(mfhastructure_obj);
						}
						
						//apres on passe a MFHA_DATAFLOW
						AMfha_dataflow mfha_dataflows = mfha_graph.getDataflows(null);
						SdaiIterator mfha_dataflows_Iterator = mfha_dataflows.createIterator();
						while(mfha_dataflows_Iterator.next()){
							MFHADataflow mfhadataflow_obj = objFactory.createMFHADataflow();
							mfhadataflow_obj.setType("MFHA_dataflow");
							EMfha_dataflow mfha_dataflow = mfha_dataflows.getCurrentMember(mfha_dataflows_Iterator);
							mfhadataflow_obj.setName(mfha_dataflow.getName(null));
							EMfha_out_interface mfha_out_interface = mfha_dataflow.getOut_data(null);
							MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
							mfhaoutinterface_obj.setType("MFHA_out_interface");
							mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
							mfhadataflow_obj.setOutData(mfhaoutinterface_obj);
							
							//Indata du dataflow
							AMfha_in_interface mfha_in_interfaces = mfha_dataflow.getIn_data(null);
							SdaiIterator mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
							SdaiIterator aux = mfha_in_interfaces.createIterator();
							if(!aux.next()){
								MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
								mfhadataflow_obj.getInData().add(mfhaininterface_obj);
							}
							else{
							while(mfha_in_interfaces_Iterator.next()){
								MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
								EMfha_in_interface mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
								mfhaininterface_obj.setName(mfha_in_interface.getName(null));
								mfhaininterface_obj.setType("MFHA_in_interface");
								mfhadataflow_obj.getInData().add(mfhaininterface_obj);
							}
							}
							
							mfhagraph_obj.getDataflow().add(mfhadataflow_obj);
						}
						
						//apres on passe a MFHA_OP
						AMfha_op mfha_ops = mfha_graph.getOps(null);
						SdaiIterator mfha_ops_Iterator = mfha_ops.createIterator();
						while(mfha_ops_Iterator.next()){
							MFHAOp mfhaop_obj = objFactory.createMFHAOp();
							mfhaop_obj.setType("MFHA_op");
							EMfha_op mfha_op = mfha_ops.getCurrentMember(mfha_ops_Iterator);
							mfhaop_obj.setName(mfha_op.getName(null));
							
							EMfha_in_interface mfha_in_interface = mfha_op.getIn_act(null);
							MFHAInInterface mfhaininterface_obj = objFactory.createMFHAInInterface();
							mfhaininterface_obj.setType("MFHA_in_interface");
							mfhaininterface_obj.setName(mfha_in_interface.getName(null));
							mfhaop_obj.setInAct(mfhaininterface_obj);
							
							EMfha_out_interface mfha_out_interface = mfha_op.getOut_act(null);
							MFHAOutInterface mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
							mfhaoutinterface_obj.setType("MFHA_out_interface");
							mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
							mfhaop_obj.setOutAct(mfhaoutinterface_obj);
							        //dataU du Op
							AMfha_in_interface mfha_in_interfaces = mfha_op.getData_u(null);
							SdaiIterator mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
							       //on utilise le aux pour pouvoir mettre une balise vide dans le cas ou on a pas d'atributs
							SdaiIterator aux = mfha_in_interfaces.createIterator();
							if(!aux.next()){
								mfhaininterface_obj = objFactory.createMFHAInInterface();
								mfhaop_obj.getDataU().add(mfhaininterface_obj);
							}
							else{
							while(mfha_in_interfaces_Iterator.next()){
								mfhaininterface_obj = objFactory.createMFHAInInterface();
								mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
								mfhaininterface_obj.setName(mfha_in_interface.getName(null));
								mfhaininterface_obj.setType("MFHA_in_interface");
								mfhaop_obj.getDataU().add(mfhaininterface_obj);
							}
							}
							
					        //conditions du Op
						mfha_in_interfaces = mfha_op.getConditions(null);
						mfha_in_interfaces_Iterator = mfha_in_interfaces.createIterator();
							//on utilise le aux pour pouvoir mettre une balise vide dans le cas ou on a pas d'atributs
					    aux = mfha_in_interfaces.createIterator();
						if(!aux.next()){
	   				      mfhaininterface_obj = objFactory.createMFHAInInterface();
    						mfhaop_obj.getConditions().add(mfhaininterface_obj);
										}
								else{
								while(mfha_in_interfaces_Iterator.next()){
									mfhaininterface_obj = objFactory.createMFHAInInterface();
									mfha_in_interface = mfha_in_interfaces.getCurrentMember(mfha_in_interfaces_Iterator);
									mfhaininterface_obj.setName(mfha_in_interface.getName(null));
									mfhaininterface_obj.setType("MFHA_in_interface");
									mfhaop_obj.getConditions().add(mfhaininterface_obj);
								}
								}							
							
							mfha_out_interface = mfha_op.getOut_data(null);
							mfhaoutinterface_obj = objFactory.createMFHAOutInterface();
							mfhaoutinterface_obj.setType("MFHA_out_interface");
							mfhaoutinterface_obj.setName(mfha_out_interface.getName(null));
							mfhaop_obj.setOutData(mfhaoutinterface_obj);
							
							mfhagraph_obj.getOp().add(mfhaop_obj);
						}
				}
			
			JAXBContext context = JAXBContext.newInstance(MFHAGraph.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
			marshaller.marshal(mfhagraph_obj, new FileWriter("firstXML.xml"));
		
		}
		session.closeSession();
	}
}
